package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test1 {

    //leetcode 生命游戏
    //https://leetcode.cn/problems/game-of-life/description/?envType=study-plan-v2&envId=top-interview-150
    private int[] row = {-1,-1,-1,0,0,1,1,1};
    private int[] col = {-1,0,1,-1,1,-1,0,1};
    public void gameOfLife(int[][] board) {
        //list记录状态相反的位置
        List<int[]> list = new ArrayList<>();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int ret = countOne(board,i,j);
                if (board[i][j] == 0) {
                    if (ret == 3) {
                        list.add(new int[]{i,j});
                    }
                }else {
                    if (ret == 2 || ret == 3) {
                        ;
                    }else {
                        list.add(new int[]{i,j});
                    }
                }
            }
        }
        //更改状态
        for (int[] tmp : list) {
            int i = tmp[0], j = tmp[1];
            board[i][j] = board[i][j] == 0 ? 1 : 0;
        }
    }

    private int countOne(int[][] board, int x, int y) {
        int count = 0;
        for (int i = 0; i < 8; i++) {
            int x1 = x + row[i];
            int y1 = y + col[i];
            if (x1 >= 0 && x1 < board.length
                    && y1 >= 0 && y1 < board[0].length
                    && board[x1][y1] == 1) count++;
        }
        return count;
    }

    public static void main(String[] args) {
        int[][] board = new int[4][3];
        board[0][1] = board[1][2] = board[2][0] = board[2][1] = board[2][2] = 1;
        Test1 test1 = new Test1();
        test1.gameOfLife(board);
        System.out.println(666);
    }
}
